package pl.edu.agh.inpema.components.globalAnalyzers.simpleGlobalAnalyzer;

import java.util.Set;

import org.apache.log4j.Logger;

import pl.edu.agh.inpema.core.components.GlobalAnalyzerUtil;
import pl.edu.agh.inpema.core.model.AnalyzeResult;
import pl.edu.agh.inpema.core.model.Analyzer;
import pl.edu.agh.inpema.core.model.Profile;
import pl.edu.agh.inpema.core.model.ProfilePair;
import pl.edu.agh.inpema.core.model.globalAnalyzer.AnalyzerResults;
import pl.edu.agh.inpema.core.model.globalAnalyzer.GlobalAnalyzeResult;
import pl.edu.agh.inpema.core.model.globalAnalyzer.GlobalAnalyzer;

public class SimpleGlobalAnalyzer implements GlobalAnalyzer{

	private GlobalAnalyzerUtil util;
	
	private static final Logger logger = Logger.getLogger(SimpleGlobalAnalyzer.class);
	
	public SimpleGlobalAnalyzer() {
		
	}
	
	public void setGlobalAnalyzerUtil(GlobalAnalyzerUtil util) {
		this.util = util;
	}
	
	public GlobalAnalyzeResult performAnalyze() {
		GlobalAnalyzeResult globalResult = new GlobalAnalyzeResult();
		
		logger.info("perform analyze");
		
		for (Analyzer analyzer : this.util.getAllAnalyzers()) {
			Set<AnalyzeResult> analyzeResultSet = this.util.getAllAnalyzeResultsForAnalyzer(analyzer);
			AnalyzerResults analyzerResults = new AnalyzerResults();
			
			for (AnalyzeResult result1 : analyzeResultSet) {
				for (AnalyzeResult result2 : analyzeResultSet) {
					if (! result1.equals(result2)) {
						Profile p1 = new Profile(result1.getReceiverName(), result1.getProfileValue(), result1.getProfileId());
						Profile p2 = new Profile(result2.getReceiverName(), result2.getProfileValue(), result2.getProfileId());
						ProfilePair profilePair = new ProfilePair(p1, p2);
						int value = analyzer.compareResults(result1, result2);
						logger.info(analyzer.getAnalyzerUtil().getAnalyzerName() + ": " + profilePair.getProfile1() + " -> " + profilePair.getProfile2() + " - " + value);
						analyzerResults.setValue(profilePair, value);
					}
				}
			}
			
			globalResult.setAnalyzerResult(analyzer.getAnalyzerUtil().getAnalyzerName(), analyzerResults);
		}
		
		return globalResult;
	}
	
}
